﻿#region Using's

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using LightPersist.Storage;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyUtils;

#endregion

namespace LightPersist.XmlStorageTests
{
	[TestClass]
	public class TestXmlStorage : TestStorage
	{
		protected const string testFolderPrefix = "_test_";

		[TestCleanup]
		public void TestXmlStorage_TearDown()
		{
			CloseStorage();
			foreach (var d in Directory.GetDirectories(Path.GetDirectoryName(StoragePath)))
			{
				if (Path.GetFileName(d).StartsWith(testFolderPrefix))
				{
					Directory.Delete(d, true);
				}
			}
		}

		protected override IStorage CreateStorage()
		{
			lastConnectionString = testFolderPrefix + Guid.NewGuid();
			var storage = new XmlStorage();
			return storage;
		}

		protected override void ReopenStorage()
		{
			base.ReopenStorage();
			sut = new XmlStorage();
			sut.Connect(lastConnectionString);
		}

		string StoragePath
		{
			get { return Path.GetFullPath(lastConnectionString); }
		}

		string TestObjectFile
		{
			get { return getFileName(testObectState); }
		}

		string getFileName(ObjectState obj)
		{
			return Path.Combine(StoragePath, obj.ClassMetainfo.Name + "_" + obj.Identity+".xml");
		}

		[TestMethod]
		public void Should_create_file_for_each_object()
		{
			////sut.Commit();
			sut.CreateObject(testObectState);
			var obj2 = PrepareNewObjectState();
			sut.CreateObject(obj2);
			//sut.Commit();

			Trace.WriteLine(TestObjectFile);
			True(TestObjectFile.EndsWith(".xml"));
			True(File.Exists(TestObjectFile));
			True(File.Exists(getFileName(obj2)));
		}

		/// <summary>
		/// This is characterisation test.
		/// </summary>
		[TestMethod]
		public void Should_create_file_of_specified_structure()
		{
			testObectState.Set("X", 1);
			sut.CreateObject(testObectState);

			var content = File.ReadAllText(TestObjectFile);
			Equal(
				@"<Object>
	<X Type=`System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089`>
		<value>1</value>
	</X>
</Object>".Arg(), content//.Replace("\r", "").Replace("\n", "")//.Replace("\t", "").Replace(" ", "")
					);

			var persisted = PrepareNewObjectState(testObjectID);
			XmlStorage.DeserializeObject(content, persisted);
			CheckValues(testObectState, persisted.PropertiesState);
		}

		[TestMethod]
		public void Should_create_folder_for_storing_date()
		{
			////sut.Commit();
			sut.CreateObject(testObectState);
			//sut.Commit();
			Trace.WriteLine(StoragePath);
			True(Directory.Exists(StoragePath));
		}

		[TestMethod, ExpectedException(typeof(IOException))]
		public void Should_not_allow_connecting_several_times_to_one_folder()
		{
			// setap has connected once
			var s = new XmlStorage();
			s.Connect(lastConnectionString);
		}

		
//		[TestMethod, Ignore]
//		public void Should_deserialize_value()
//		{
//			Equal(1, XmlStorage.DeserializeValue("1", typeof(int)));
//			Equal(3d, XmlStorage.DeserializeValue("3", typeof(double)));
//			Equal(4f, XmlStorage.DeserializeValue("4", typeof(float)));
//			Equal("hello", XmlStorage.DeserializeValue("hello", typeof(string)));
//			Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-ru");
//			Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-ru");
//			Equal(double.PositiveInfinity, XmlStorage.DeserializeValue("Infinity", typeof(double)));
//			Equal(double.NegativeInfinity, XmlStorage.DeserializeValue("-Infinity", typeof(double)));
//			Equal(double.NaN, XmlStorage.DeserializeValue("NaN", typeof(double)));
//		}

	}
}